package com.jw.no3_registration;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity
    implements View.OnClickListener,RadioGroup.OnCheckedChangeListener, CompoundButton.OnCheckedChangeListener
    {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        initView();
    }

    private EditText name,email,password;
    private Button submit;
    private RadioGroup sex;
    private CheckBox sing,dance,read;

    private String nameString,emailString,passwordString;
    private String[] sexs = {"男","女"};
    private int sexInt=0;
    private String hobbys="";
    private void initView(){
        //通过ID获得控件
        name = findViewById(R.id.inputname);
        email = findViewById(R.id.inputemail);
        password = findViewById(R.id.inputpassword);
        submit = findViewById(R.id.submit);
        sex = findViewById(R.id.sex);
        sing = findViewById(R.id.sing);
        dance = findViewById(R.id.dance);
        read = findViewById(R.id.read);

        //设置控件的事件
        submit.setOnClickListener(this);
        sex.setOnCheckedChangeListener(this);
        sing.setOnCheckedChangeListener(this);
        dance.setOnCheckedChangeListener(this);
        read.setOnCheckedChangeListener(this);

        //默认选择男性
        sex.check(R.id.man);
    }

    /** 判断输入是否正确 **/
    private String editIsRight(){
        if(nameString==null||nameString.isEmpty())
            return "请输入名字!!!";
        if(emailString==null||emailString.isEmpty())
            return "请输入邮箱!!!";
        if(passwordString==null||passwordString.isEmpty())
            return "请输入密码!!!";
        return null;
    }

    private void showMessage(String str){
        Toast toast = Toast.makeText(this,str,Toast.LENGTH_LONG);
        toast.setGravity(Gravity.CENTER,0,0);
        toast.show();
    }
        /** button 控件的点击事件 **/
        @Override
        public void onClick(View view) {
            nameString = name.getText().toString();
            emailString = email.getText().toString();
            passwordString = password.getText().toString();
            String errorMsg = editIsRight();
            if(errorMsg!=null)
            {
                showMessage(errorMsg);
                return;
            }
            String message="";
            message += "名字:"+nameString+"\n";
            message += "邮箱:"+emailString+"\n";
            message += "密码:"+passwordString+"\n";
            message += "性别:"+sexs[sexInt]+"\n";
            message += "爱好:"+hobbys+"\n";

            showMessage(message);
        }

        /** 多选框的点击事件 **/
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            //获得选中控件的文本内容
            String text = compoundButton.getText().toString()+" ";
            if(b){
                //选中
                if(!hobbys.contains(text)){
                    //hobbys文本中不包含选中的字符
                    hobbys+=text;
                }
            }else{
                //取消
                if(hobbys.contains(text)){
                    //hobbys文本中包含选中的字符 ,删除该兴趣
                    hobbys=hobbys.replace(text,"");
                }
            }
        }

        /** 单选框的点击事件 **/
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, int id) {
            if(id==R.id.man){
                //选择男
                sexInt = 0;
            }else{
                //选择女
                sexInt = 1;
            }
        }
    }